﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sayeet
{
    public class RosterItem
    {
        private JID jid;
        private string name;
        private enSubscription subscription = enSubscription.to;
        private string group;
        private Chat activeChat = null;
        private XMPPConnection.Status status = XMPPConnection.Status.offline;
        AppData appData;

        public enum enSubscription
        {
            both,
            from,
            to,
            none
        }


        public RosterItem(AppData ap)
        {
            appData = ap;
        }
        /* no longer used
        public RosterItem(JID _jid, string _name, enSubscription _subscription, 
            string _group, XMPPConnection.Status _status, AppData ap)
        {
            jid = _jid;
            name = _name;
            subscription = _subscription;
            group = _group;
            status = _status;
            appData = ap;
        }
         * */

        
        public RosterItem(JID _jid, string _name, string _group, AppData ap)
        {
            jid = _jid;
            name = _name;
            group = _group;
            appData = ap;
        }
        

        public JID Jid
        {
            get
            {
                return jid;
            }
            set
            {
                jid = value;
            }
        }

        public string Name
        {
            get
            {
                if (name == "" || name == null)
                    return jid.ToStringWithoutResource();
                else
                    return name;
            }
            set
            {
                name = value;
            }
        }

        public string Group
        {
            get
            {
                if (group == "")
                    return "Ungrouped";
                else
                    return group;
            }
            set
            {
                group = value;
            }
        }

        public enSubscription Subscription
        {
            get
            {
                return subscription;
            }
            set
            {
                subscription = value;
            }
        }

        public Chat ActiveChat
        {
            get
            {
                return activeChat;
            }
            set
            {
                activeChat = value;
            }
        }

        public XMPPConnection.Status Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }
        }

        public void startChat()
        {
            if (activeChat == null)
            {
                activeChat = new Chat(this, appData);
            }
            System.Windows.Forms.TabPage tabPage = (System.Windows.Forms.TabPage)activeChat.pane.Parent;
            System.Windows.Forms.TabControl tabControl = (System.Windows.Forms.TabControl)tabPage.Parent;
            ChatWindow chatWindow = (ChatWindow)tabControl.Parent;
            chatWindow.BringToFront();
            tabControl.SelectTab(tabPage);
        }

        public void receiveMessage(string message)
        {
            startChat();
            activeChat.pane.addMessage(message, ChatPane.MessageType.incoming);
        }

        public void sendMessage(string message)
        {
            appData.SendChatMessage(jid, message);
        }

        public void SetStatus(XMPPConnection.Status st)
        {
            status = st;
            appData.mainWindow.updateList();
        }
    }
}
